package com.xiyuan.smartutils.client.domain;

import java.util.HashMap;
import java.util.Map;

/**
 * the description for class Enctype
 *
 * @author xiyuan-lgz 2025-08-19 @version v1.0.0 新建与整理
 */
public enum ContentType {
    
    /**
     * x-www-form-urlencoded
     */
    APPLICATION_X_WWW_FORM("application/x-www-form-urlencoded"),
    
    /**
     * multipart/form-data
     */
    MULTIPART_FORM_DATA("multipart/form-data"),
    /**
     * application/json
     */
    APPLICATION_JSON("application/json"),
    
    /**
     * text/plain
     */
    TEXT_PLAIN("text/plain"),
    
    APPLICATION_OCTET_STREAM("application/octet-stream"),
    
    //
    ;
    
    private final String val;
    
    ContentType(String value) {
        this.val = value;
    }
    
    public String value() {
        return this.val;
    }
    
    @Override
    public String toString() {
        return this.value();
    }
    
    private static final Map<String,ContentType> enctypeMap = new HashMap<String, ContentType>();
    
    static {
        for (ContentType b : ContentType.values()) {
            enctypeMap.put(b.val, b);
        }
    }
    
    public static boolean isSupport(String contentType) {
        return enctypeMap.containsKey(String.valueOf(contentType).toLowerCase());
    }
    
    /**
     * @param contentType enctype
     * @return Enctype instance
     * @throws IllegalArgumentException exception when unsupport contentType
     */
    public static ContentType of(String contentType) throws IllegalArgumentException {
        ContentType type = enctypeMap.get(String.valueOf(contentType).toLowerCase());
        if (type == null) {
            throw new IllegalArgumentException("unsupport enctype: " + contentType);
        }
        
        return type;
    }
}
